package com.raymond.cmpp.codec.cmpp3.encodeing;

import com.raymond.client.codec.encodeing.BaseEncoder;
import com.raymond.client.constants.GlobalConstants;
import com.raymond.client.msg.BaseMessage;
import com.raymond.client.utils.NettyByteBufUtil;
import com.raymond.cmpp.codec.annotation.MsgCodec;
import com.raymond.cmpp.enums.CommandIdType;
import com.raymond.cmpp.msg.request.SubmitRequestMessage;
import com.raymond.cmpp.utils.CommonUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * 短信提交请求编码
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-05 18:01
 */
@MsgCodec(commandId = CommandIdType.SUBMIT_REQUEST)
public class SubmitRequestMessageEncoder extends BaseEncoder {

    @Override
    public byte[] encode0(BaseMessage msg) throws Exception {
        SubmitRequestMessage requestMessage = (SubmitRequestMessage)msg;
        ByteBuf bodyBuffer = Unpooled.buffer();

        bodyBuffer.writeLong(requestMessage.getMsgId());

        bodyBuffer.writeByte(requestMessage.getPkTotal());
        bodyBuffer.writeByte(requestMessage.getPkNumber());
        bodyBuffer.writeByte(requestMessage.getRegisteredDelivery());
        bodyBuffer.writeByte(requestMessage.getMsgLevel());

        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getServiceId().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 10));

        bodyBuffer.writeByte(requestMessage.getFeeUserType());

        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getFeeTerminalId().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 32));
        bodyBuffer.writeByte(requestMessage.getFeeTerminalType());
        bodyBuffer.writeByte(requestMessage.getTpPid());
        bodyBuffer.writeByte(requestMessage.getTpUdhi());
        bodyBuffer.writeByte(requestMessage.getMsgFmt());

        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getMsgSrc().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET),
                6));

        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getFeeType().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET),
                2));

        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getFeeCode().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET),
                6));

        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getValIdTime().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET),
                17));

        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getAtTime().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET),
                17));

        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getSrcId().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET),
                21));
        String[] destTerminalIds = requestMessage.getDestTerminalId();
        bodyBuffer.writeByte(requestMessage.getDestTerminalId().length);
        for (String destTermId : destTerminalIds) {
            bodyBuffer.writeBytes(CommonUtil.ensureLength(destTermId.getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET),
                    32));
        }

        bodyBuffer.writeByte(requestMessage.getDestTerminalType());

        byte[] msgContentBytes = CommonUtil.getMsgContentBytes((byte) requestMessage.getMsgFmt(), requestMessage.getMsgContent());
        bodyBuffer.writeByte(msgContentBytes.length);

        bodyBuffer.writeBytes(msgContentBytes);

        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getLinkId().getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET),
                20));

        return NettyByteBufUtil.toArray(bodyBuffer, bodyBuffer.readableBytes());
    }
}
